package com.hzit.user.controller;

import com.hzit.common.R;
import com.hzit.user.client.ManagerClient;
import com.hzit.user.entity.User;
import com.hzit.user.service.IUserService;
import com.hzit.user.to.BrandTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.data.ConditionalOnRepositoryType;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	private IUserService userService;
	@Autowired
	private ManagerClient client;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;


	@GetMapping("/list")
	public R list(){
		return R.ok().put("list",userService.list());
	}

	@PostMapping("/save")
	public R save(@RequestBody User user){
		userService.save(user);
		return R.ok();
	}

	@GetMapping("/info")
	public R info(){
		String header = request.getHeader("h-01");
		String color = request.getParameter("color");
		String welcomeHeader = request.getHeader("welcome-header");

		//在方法中只能得到请求过来的信息，而不能得到响应出去的信息
		// String respHeader = response.getHeader("X-Response-Red");
		// System.out.println("respHeader = " + respHeader);
		return R.ok()
				// .put("username",username)
				// .put("age",age)
				.put("header",header)
				.put("welcomeHeader",welcomeHeader)
				.put("color",color);
	}

	//1. 查询品牌列表
	@GetMapping("/brand/list")
	public R findBrandList(){
		String username = request.getHeader("username");
		System.out.println("username[username] = " + username);
		//响应时会携带着cookie的值
		String cv = "2023-8-23日创建cookie";
		response.addCookie(new Cookie("c1",cv));
		return client.findAll(cv);
	}

	//2. 查询单个品牌
	@GetMapping("/brand/findById/{id}")
	public R findById(@PathVariable Long id){
		return client.findById(id);
	}

	//3. 添加品牌
	@PostMapping("/brand/save")
	public R saveBrand(@RequestBody BrandTo to){
		return client.add(to);
	}
}
